﻿

// ======================
// weights  population
db.CompanyRatingsB.mapReduce(

function () {


    doc = this;



    //  print(doc.CompanyName);



    var key = doc._id;

    var values = doc;



    var sumInd3 = 0.0;

    var sumThemesWeights = 0.0;

    for (var idx = 0; idx < doc.Themes.length; idx++)

        sumThemesWeights += doc.Themes[idx].WeightRelative;





    var sumThemesWeights2 = 0.0;

    for (var idx = 0; idx < doc.Themes.length; idx++) {



        values.Themes[idx].WeightPercent = doc.Themes[idx].WeightRelative / sumThemesWeights;

        sumThemesWeights2 += values.Themes[idx].WeightPercent;

        //print( values.Themes[idx].Name + ": " + values.Themes[idx].WeightPercent);

    }

    //  print( "Total themes: " + sumThemesWeights2);



    for (var idx = 0; idx < doc.Themes.length; idx++) {



        var th = values.Themes[idx];



        //   print( "Theme: " + th.Name + ", weight: " + th.WeightPercent);



        var sumTopic = 0.0;

        for (var j = 0; j < doc.Topics.length; j++) {

            if (th._id.hex() == doc.Topics[j].ThemeId.hex())

                sumTopic += doc.Topics[j].WeightRelative;

        }





        var sumTopic2 = 0.0;

        var unit = th.WeightPercent / sumTopic;

        for (var j = 0; j < doc.Topics.length; j++) {

            if (th._id.hex() == doc.Topics[j].ThemeId.hex()) {



                values.Topics[j].WeightPercent = doc.Topics[j].WeightRelative * unit;

                sumTopic2 += values.Topics[j].WeightPercent;

                // print( th.Name + "   " +  values.Topics[j].Name + " " + values.Topics[j].WeightPercent );

            }

        }

        //    print( th.Name + " total topics: " +  sumTopic2);



    }





    for (var idx = 0; idx < doc.Topics.length; idx++) {



        var to = values.Topics[idx];

        //   print( "Topic: " + to.Name + ", weight: " + to.WeightPercent );

        if (isNaN(to.WeightPercent))

            continue;



        var sumInd = 0.0;

        var sumInd2 = 0.0;

        for (var j = 0; j < doc.Indicators.length; j++) {

            if (to._id.hex() == doc.Indicators[j].TopicId.hex()) {

                sumInd += doc.Indicators[j].WeightRelative;

            }

        }



        var unit = to.WeightPercent / sumInd;

        for (var j = 0; j < doc.Indicators.length; j++) {

            if (to._id.hex() == doc.Indicators[j].TopicId.hex()) {

                values.Indicators[j].WeightPercent = doc.Indicators[j].WeightRelative * unit;

                sumInd2 += values.Indicators[j].WeightPercent;





                sumInd3 += values.Indicators[j].WeightPercent;



                //         print( to.Name + " " +  values.Indicators[j].Code + " " + values.Indicators[j].WeightPercent );

            }

        }



        //   print( to.Name + " total indicators per topic: " +  sumInd2);

    }



    //print( doc.CompanyName + " total indicators: " +  sumInd3);



    emit(key, values);

},

function (key, value) { },
{
    out: "weights",
}
);



//  populate   weights 

db.system.js.save({ id: "computeWeights", value:"function(){ ;}" });
db.eval("computeWeights()");



//  generate  Theme scores 
//  generate  Topic scores 
//  generate  Indicator scores 


// generate  ranking 